#include <iostream>
#include <set>
#include <map>
#include <utility>
#include <string>

using std::cout;
using std::endl;
using std::set;
using std::map;
using std::pair;
using std::string;
using std::make_pair;

void test()
{
    /* int arr[10] = {1, 2, 3, 5} */
    /* vector<int> vec; */

    //set的特征
    //1、set中存放的元素是唯一的，不能重复
    //2、默认情况下，会按照元素进行升序排列(从小到大)
    //3、set的底层使用的是红黑树
    //创建对象number
    set<int> number = {1, 3, 6, 9, 7, 6, 4, 2, 6};

    //遍历set中的元素
    //迭代器,就是一种指针，称为泛型指针
    set<int>::iterator it;
    for(it = number.begin(); it != number.end(); ++it)
    {
        cout << *it << "  ";
    }
    cout << endl;

    //遍历方式，C++11
    //auto可以让编译器进行推导elem的类型
    for(auto &elem : number)
    {
        cout << elem << "  ";
    }
    cout << endl;

    cout << endl << "set中进行查找操作" << endl;
    size_t cnt1 = number.count(10);
    size_t cnt2 = number.count(6);
    cout << "cnt1 = " << cnt1 << endl
         << "cnt2 = " << cnt2 << endl;

    /* set<int>::iterator it2 = number.find(5); */
    /* auto it2 = number.find(5); */
    auto it2 = number.find(7);
    if(it2 == number.end())//迭代器判空可以使用end
    {
        cout << "该元素不存在set中  " << endl;
    }
    else
    {
        cout << "该元素存在set中 ：" << *it2 << endl;
    }

    cout << endl << "set的插入操作" << endl;
    /* auto ret = number.insert(8); */
    pair<set<int>::iterator, bool> ret = number.insert(8);
    if(ret.second)
    {
        cout << "该元素插入set成功 " 
             << *(ret.first) << endl;
    }
    else
    {
        cout << "该元素插入不成功，表明该元素存在" << endl;
    }
    for(auto &elem : number)
    {
        cout << elem << "  ";
    }
    cout << endl;

    cout << endl;
    int arr[10] = {1, 2, 77, 9, 33, 22, 55, 77, 33, 99};
    number.insert(arr, arr + 10);//左闭右开,[arr, arr + 10)
    for(auto &elem : number)
    {
        cout << elem << "  ";
    }
    cout << endl;

    cout << endl << "set下标" << endl;
    /* cout << "number[0] = " << number[0] << endl;//不支持下标操作 */

    cout << endl << endl;
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    /* *it = 300;//为了保证红黑树的结构稳定，不允许直接修改 */
}

void test00()
{
    pair<int, string> number = {1, "wangdao"};
    cout << number.first << "   " << number.second << endl;
}

void test2()
{
    //map的特征
    //1、存放的是key-value类型，也就是pair类型，key值
    //是唯一的，不能重复，但是value值是可以重复的
    //2、默认情况下，会按照key值进行升序排列（从小到大）
    //3、map的底层也是使用红黑树
    //初始化
    map<int, string> number = {
        {1, "hello"},
        {3, "world"},
        {2, "wangdao"},
        pair<int, string>(6, "wuhan"),
        pair<int, string>(4, "hubei"),
        make_pair(9, "beijing"),
        make_pair(3, "world"),
        make_pair(3, "nanjing"),
        make_pair(7, "world"),
    };

    //遍历map中的元素
    //迭代器,就是一种指针，称为泛型指针
    map<int, string>::iterator it;
    for(it = number.begin(); it != number.end(); ++it)
    {
        cout << it->first << "  "  << it->second << endl;
    }
    cout << endl << endl;

    //遍历方式，C++11
    //auto可以让编译器进行推导elem的类型
    for(auto &elem : number)
    {
        cout << elem.first << "  " << elem.second << endl;
    }
    cout << endl;

    cout << endl << "map中进行查找操作" << endl;
    size_t cnt1 = number.count(10);
    size_t cnt2 = number.count(6);
    cout << "cnt1 = " << cnt1 << endl
         << "cnt2 = " << cnt2 << endl;

    /* map<int, string>::iterator it2 = number.find(5); */
    /* auto it2 = number.find(5); */
    auto it2 = number.find(7);
    if(it2 == number.end())//迭代器判空可以使用end
    {
        cout << "该元素不存在map中  " << endl;
    }
    else
    {
        cout << "该元素存在map中 ：" 
             << it2->first 
             << "  " <<it2->second
             << endl;
    }

    cout << endl << "map的插入操作" << endl;
    pair<map<int, string>::iterator, bool> ret = 
        /* number.insert(pair<int, string>(8, "point")); */
        number.insert(make_pair(8, "point"));
        /* number.insert({8, "point"}); */
    if(ret.second)
    {
        cout << "该元素插入成功 " 
             << ret.first->first
             << "   "
             << ret.first->second << endl;
    }
    else
    {
        cout << "该元素插入不成功，该元素已经存在map中"  << endl;
    }
    for(auto &elem : number)
    {
        cout << elem.first << "  " << elem.second << endl;
    }
    cout << endl;

    cout << endl << "map的下标操作" << endl;
    cout << "number[1] = " << number[1] << endl;//查找
    cout << "number[5] = " << number[5] << endl;//插入
    for(auto &elem : number)
    {
        cout << elem.first << "  " << elem.second << endl;
    }
    cout << endl << endl << endl;
    number[5] = "kiki";//修改
    for(auto &elem : number)
    {
        cout << elem.first << "  " << elem.second << endl;
    }
    cout << endl;
}

int main(int argc, char **argv)
{
    test2();
    return 0;
}

